package com.woniu.park.config;

/**
 * @author 张洛诚
 * @date 2019/8/29 21:10:43
 * @description
 */
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class RabbitMQConfig  {
    /**
     * 注册RabbitTemplate
     * @param connectionFactory
     * @return
     */
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setConfirmCallback(confirmCallback);
        rabbitTemplate.setReturnCallback(returnCallback);
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setMessageConverter(messageConverter());
        return rabbitTemplate;
    }
    /**
     * 注册exchange
     * @return
     */
    @Bean
    public FanoutExchange exchange(){
        return new FanoutExchange("logmodel_fanoutExchange");
    }
    /**
     * 消息序列化转换器
     */
    public Jackson2JsonMessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
    /**
     * 配置confirmCallback
     */
    RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {
        /**
         * Confirmation callback.
         *
         * @param correlationData correlation data for the callback.
         * @param ack             true for ack, false for nack
         * @param cause           An optional cause, for nack, when available, otherwise null.
         */
        @Override
        public void confirm(CorrelationData correlationData, boolean ack, String cause) {
            String  correlationDataId=null;
            if(correlationData!=null){
                correlationDataId = correlationData.getId();
            }
            if(ack){
                //接收成功消息
                log.info("接收成功消息Id："+correlationDataId);
            }else{
                //接收不成功
                log.warn("接收不成功消息Id："+correlationDataId+",不成功原因："+cause);
            }
        }
    };
    /**
     * 配置returnCallback
     */
    RabbitTemplate.ReturnCallback returnCallback = new RabbitTemplate.ReturnCallback() {
        /**
         * Returned message callback.
         *
         * @param message    the returned message.
         * @param replyCode  the reply code.
         * @param replyText  the reply text.
         * @param exchange   the exchange.
         * @param routingKey the routing key.
         */
        @Override
        public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
            String msg = new String(message.getBody());
            log.warn("路由失败消息：" + msg, replyCode, replyText, exchange, routingKey);
        }
    };

}

